package com.damie.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.damie.common.jwtuilts.JwtUtil;
import com.damie.domain.WasteOrder;
import com.damie.domain.WasteOrderComplain;
import com.damie.domain.WasteRating;
import com.damie.dto.SitePageDto;
import com.damie.exception.MyException;
import com.damie.mapper.WasteOrderComplainMapper;
import com.damie.mapper.WasteOrderMapper;
import com.damie.mapper.WasteRatingMapper;
import com.damie.param.*;
import com.damie.service.WasteRatingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.jsonwebtoken.Claims;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Alon
 * @since 2021-01-25
 */
@Service
public class WasteRatingServiceImpl extends ServiceImpl<WasteRatingMapper, WasteRating> implements WasteRatingService {

    @Resource
    private WasteOrderMapper wasteOrderMapper;

    @Resource
    private WasteOrderComplainMapper wocm;

    @Resource
    private WasteRatingMapper wasteRatingMapper;
    //投诉订单消息队列
    @Autowired
    private RocketMQTemplate rmt;

    /**
     * 订单评价
     * @param wasteRatingParam
     * @return
     */
    @Override
    public Boolean insertRating(WasteRatingParam wasteRatingParam, HttpServletRequest request) {
        WasteOrder wasteOrder = wasteOrderMapper.selectOne(new QueryWrapper<WasteOrder>().eq("waste_order_id", wasteRatingParam.getRatingOrderId()));
        if (ObjectUtils.isEmpty(wasteOrder)){
            throw new MyException(20011,"订单不存在");
        }
        if (wasteOrder.getWasteOrderStatus() == 3){
            WasteRating wasteRating = new WasteRating();
            BeanUtils.copyProperties(wasteRatingParam,wasteRating);
            wasteRating.setRatingStatus(1);
            JSONObject jb = new JSONObject();
            for (int i = 0; i < wasteRatingParam.getRatingPicture().length; i++) {
                jb.put("第"+(i+1)+"张",wasteRatingParam.getRatingPicture()[i]);
            }
            wasteRating.setRatingPicture(jb.toJSONString());

            //获取当前登录的token
            String token = request.getHeader("token");
            Claims claims = JwtUtil.parseToken(token);
            Object userId = claims.get("userId");
            String id = userId.toString();


            //用户id
            wasteRating.setRatingUserId(id);
            save(wasteRating);
            return true;
        }
        return false;
    }

    /**
     * 订单投诉
     * @return
     */
    @Override
    public Boolean complain(WasteComplainParam wasteComplainParam) {
        WasteOrder wasteOrder = wasteOrderMapper.selectOne(new QueryWrapper<WasteOrder>().eq("waste_order_id", wasteComplainParam.getWasteOrderComplainOrderId()));
        if (ObjectUtils.isEmpty(wasteOrder)){
            throw new MyException(20014,"该订单不存在");
        }
        if (wasteOrder.getWasteOrderStatus() == 3){
            WasteOrderComplain wasteOrderComplain = new WasteOrderComplain();
            BeanUtils.copyProperties(wasteComplainParam,wasteOrderComplain);
            JSONObject jb = new JSONObject();
            for (int i = 0; i < wasteComplainParam.getWasteOrderComplainPicture().length; i++) {
                jb.put("第"+(i+1)+"张",wasteComplainParam.getWasteOrderComplainPicture()[i]);
            }
            wasteOrderComplain.setWasteOrderComplainPicture(jb.toJSONString());
            wasteOrderComplain.setWasteOrderComplainStatus(1);
            wocm.insert(wasteOrderComplain);
            //发送单项消息
            rmt.sendOneWay("message:order_complain", MessageBuilder.withPayload(wasteOrderComplain)
            .setHeader("KEYS", UUID.randomUUID().toString()).build());
            System.out.println("单项消息发送成功");
            return true;
        }
        return false;
    }

    /**
     * 用户追加评论
     * @param commentParam
     * @return
     */
    @Override
    public Boolean modification(CommentParam commentParam) {
        WasteRating wasteRating = getOne(new QueryWrapper<WasteRating>().eq("rating_order_id",commentParam.getOrderId()));
        if (ObjectUtils.isEmpty(wasteRating)){
            throw new MyException(20015,"该订单不存在");
        }
        wasteRating.setRatingOpened(commentParam.getModify());
        updateById(wasteRating);
        return true;
    }

    /**
     * 删除评论
     * @param orderId
     * @return
     */
    @Override
    public Boolean deletedComment(String orderId) {
        WasteRating wasteRating = getOne(new QueryWrapper<WasteRating>().eq("rating_order_id",orderId));
        if (ObjectUtils.isEmpty(wasteRating)){
            throw new MyException(20015,"该订单不存哎");
        }
        removeById(wasteRating);
        return true;
    }

    /**
     * 站点回复评论
     * @param resultSiteParam
     * @return
     */
    @Override
    public Boolean siteComment(ResultSiteParam resultSiteParam) {
        WasteRating wasteRating = getOne(new QueryWrapper<WasteRating>().eq("rating_order_id",resultSiteParam.getOrderId()));
        if (ObjectUtils.isEmpty(wasteRating)){
            throw new MyException(20015,"该订单不存哎");
        }
        wasteRating.setResultComment(resultSiteParam.getResultComment());
        updateById(wasteRating);
        return true;
    }

    /**
     * 查询当前站点的所有订单评论
     * @param sitePageParam
     */
    @Override
    public List<WasteRating> selectSite(SitePageParam sitePageParam,HttpServletRequest request){

        //获取当前登录的token
        String token = request.getHeader("token");
        Claims claims = JwtUtil.parseToken(token);
        Object userId = claims.get("userId");
        String id = userId.toString();

        sitePageParam.setSiteId(id);

        List<WasteRating> wasteRatings = wasteRatingMapper.selectSite(sitePageParam.getSiteId(),sitePageParam.getCurrent(),sitePageParam.getSize());
        if (ObjectUtils.isEmpty(wasteRatings)){
            throw new MyException(20015,"当前并无任何订单");
        }
        return wasteRatings;
    }

}
